package com.shopx.gw.fliter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * 解决拦截器读取参数后，controller无法二次读取的问题
 *
 * @author:lituo
 * @date:2023/3/12
 **/
//@WebFilter(urlPatterns = "/*", filterName = "yqzFilter")
public class YqzFilter implements Filter {
  @Override
  public void init(FilterConfig filterConfig) throws ServletException {

  }

  @Override
  public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
    // 防止流读取一次后就没有了, 所以需要将流继续写出去
    HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
    ServletRequest requestWrapper = new BodyReaderHttpServletRequestWrapper(httpServletRequest);
    filterChain.doFilter(requestWrapper, servletResponse);
  }

  @Override
  public void destroy() {

  }

}
